import 'package:myapp/api/online_repository.dart';
import 'package:myapp/bean/article.dart';
import 'package:myapp/bean/category.dart';
import 'package:myapp/globle/const.dart';
import 'package:myapp/ui/home_page.dart';
import 'package:myapp/view_model/base_model.dart';

class HomePageModel extends BaseModel {
  List<Article> _articles = [];
  List<Article> _topArticles = [];
  List<Category> _tags = [];
  bool _isLoadMore = false;

  List<Article> get getTopArticles => _topArticles;

  List<Article> get getArticles => _articles;

  List<Category> get getTags => _tags;

  bool get isLoadMore => _isLoadMore;

  int _page = 0;

  int _type;

  int _cid = -1;

  int get cid => _cid;

  HomePageModel(this._type);

  refresh() async {
    _page = 0;
    _articles.clear();
    if (_type == NEWEST) {
      var articles = await OnlineRepository.getProjectList(_page);
      _articles.addAll(articles);
    } else if (_type == SQUARE) {
      var articles = await OnlineRepository.getUserArticleList(_page);
      _articles.addAll(articles);
    } else if (_type == PROJECT || _type == ARTICLE) {
      if (_tags.length == 0) {
        List<Category> tags = _type == PROJECT
            ? await OnlineRepository.getProjectCategories()
            : await OnlineRepository.getWechatCategories();
        _tags.addAll(tags);
        if (_tags.isNotEmpty && _cid == -1) {
          _cid = _tags[0].id;
        }
      }
      if (_cid != -1) {
        var articles = _type == PROJECT
            ? await OnlineRepository.getProjectListByCid(_page, _cid)
            : await OnlineRepository.getWechatArticleList(_page, _cid);
        _articles.addAll(articles);
      }
    } else {
      var topArticles = await OnlineRepository.fetchTopArticles();
      topArticles?.forEach((element) {
        element.top = true;
      });
      _articles.addAll(topArticles);
      var articles = await OnlineRepository.fetchArticles(_page);
      _articles.addAll(articles);
    }
    _isLoadMore = _articles.length >= Const.PAGE_NUM;
    notifyListeners();
  }

  loadMore() async {
    _page++;
    List<Article> articles;
    if (_type == NEWEST) {
      articles = await OnlineRepository.getProjectList(_page);
      _articles.addAll(articles);
    } else if (_type == SQUARE) {
      articles = await OnlineRepository.getUserArticleList(_page);
      _articles.addAll(articles);
    } else if (_type == PROJECT || _type == ARTICLE) {
      if (_cid != -1) {
        articles = _type == PROJECT
            ? await OnlineRepository.getProjectListByCid(_page, _cid)
            : await OnlineRepository.getWechatArticleList(_page, _cid);
        _articles.addAll(articles);
      } else {
        articles = [];
      }
    } else {
      articles = await OnlineRepository.fetchArticles(_page);
      _articles.addAll(articles);
    }
    _isLoadMore = articles.length >= Const.PAGE_NUM;
    notifyListeners();
  }

  void chooseTag(int cid) {
    _cid = cid;
  }

  @override
  void dispose() {
    super.dispose();
    print("HomePageModel-->>dispose");
  }
}
